/// 
/// Services
/// -------------------------------------------------------------------------------------------------------------------
/// <reference path="_references.ts" />

//// Demonstrate how to register services. In this case it is a simple value service.
angular.module('app.services', ['ngResource'])
  .value('version', '0.1')
      .factory('logger', function (toastr, $window) {

          // This logger wraps the toastr logger and also logs to console
          // toastr.js is library by John Papa that shows messages in pop up toast.
          // https://github.com/CodeSeven/toastr

          toastr.options.timeOut = 7500; // 3 second toast timeout
          toastr.options.positionClass = 'toast-bottom-right';

          var logger = {
              error: error,
              info: info,
              success: success,
              warning: warning,
              log: log // straight to console; bypass toast
          };

          function error(message, title) {
              toastr.error(message, title);
              console.log("Error: " + message);
          };
          function info(message, title) {
              toastr.info(message, title);
              console.log("Info: " + message);
          };
          function success(message, title) {
              toastr.success(message, title);
              console.log("Success: " + message);
          };
          function warning(message, title) {
              toastr.warning(message, title);
              console.log("Warning: " + message);
          };

          // IE and google chrome workaround
          // http://code.google.com/p/chromium/issues/detail?id=48662
          function log() {
              var console = $window.console;
              !!console && console.log && console.log.apply && console.log.apply(console, arguments);
          }

          return logger;
      })
    .factory('AdminServices', function ($resource) {
      //resource location as per webapiconfig.cs file
      var AdminServices = $resource(
          '/api/:controller/:id',
          [],
          {
              GetByDesc: { method: 'GET', params: { controller: 'admin' }, isArray: true },
              Get: { method: 'GET', params: { controller: 'admin' } },
              Post: { method: 'POST', params: { controller: 'admin' } },
              Put: { method: 'PUT', params: { controller: 'admin', id: '@id' } },
              Delete: { method: 'DELETE', params: { controller: 'admin', id: '@id' } },
          }
          );
      return AdminServices;
  })
    .factory('TeamServices', function ($resource) {
        //resource location as per webapiconfig.cs file
        var TeamServices = $resource(
            '/api/:controller/:id',
            [],
            {
                GetTeams: { method: 'GET', params: { controller: 'team' }, isArray: true },
                GetTeam: { method: 'GET', params: { controller: 'teamDetail' } },
                AddTeam: { method: 'POST', params: { controller: 'team' } },
                PostMember: { method: 'POST', params: { controller: 'teamDetail' } },
                PutTeam: { method: 'PUT', params: { controller: 'teamDetail', id: '@id' } },
                DeleteTeam: { method: 'DELETE', params: { controller: 'team', id: '@id' } },
                DeleteTeamMember: { method: 'DELETE', params: { controller: 'teamDetail', id: '@id' } },
            }
            );
        return TeamServices;
    })
    .factory('TeamMemberStateService', function()
    {
        var Member;
        var getMember = function () {
            return Member;
        };
        var setMember = function (m) {
            Member = m;
        };

        return {
            getMember: getMember,
            setMember: setMember
        }
    })
    .factory('IncidentServices', function ($resource) {
        //resource location as per webapiconfig.cs file
        var IncidentServices = $resource(
            '/api/:controller/:id',
            [],
            {
                GetIncidents: { method: 'GET', params: { controller: 'incident' }, isArray: true },
                GetIncident: { method: 'GET', params: { controller: 'incidentDetail' } },
                GetLocation: { method: 'GET', params: { controller: 'locationDetail' } },
                PostIncident: { method: 'POST', params: { controller: 'incidentDetail' } },
                PutIncident: { method: 'PUT', params: { controller: 'incidentDetail', id: '@id' } },
                DeleteIncident: { method: 'DELETE', params: { controller: 'incidentDetail', id: '@id' } },
            }
            );
        return IncidentServices;
    })
   
     .factory('LocationServices', function ($resource) {
         //resource location as per webapiconfig.cs file
         var LocationServices = $resource(
             '/api/:controller/:id',
             [],
             {
                 GetLocation: { method: 'GET', params: { controller: 'locationDetail', id: '@id' } },
                 GetNewLocation: { method: 'GET', params: { controller: 'locationDetail',  incidentId: '@incidentId' } },
                 GetLatLong: { method: 'GET', params: { controller: 'locationDetail', address: '@address' }, isArray: true },
                 PostLocation: { method: 'POST', params: { controller: 'locationDetail' } },
                // PutVictim: { method: 'PUT', params: { controller: 'locationDetail', id: '@id' } },
                 PutLocation: { method: 'PUT', params: { controller: 'locationDetail', id: '@id' } },
                 DeleteLocation: { method: 'DELETE', params: { controller: 'locationDetail', id: '@id' } },
             }
             );
         return LocationServices;
     })
  
     .factory('VictimsServices', function ($resource) {
         var VictimsServices = $resource(
             '/api/:controller/:id',
             [],
             {
                 GetVictims: { method: 'GET', params: { controller: 'victims', id: '@id' }, isArray: true },
                 //GetNewVictim: { method: 'GET', params: { controller: 'victims', locationId: '@locationId' } },
                 //GetVictim: { method: 'GET', params: { controller: 'victims', id: '@id' }},
                 PostVictim: { method: 'POST', params: { controller: 'victims' } },
                 PutVictim: { method: 'PUT', params: { controller: 'victims', id: '@id' } },
                 DeleteVictim: { method: 'DELETE', params: { controller: 'victims', id: '@id' } },
             }
             );
         return VictimsServices;
     })


.factory('Users', function ($http) {
    return {
        getAll: function (success, error) {
            $http.get('/users').success(success).error(error);
        }
    };
});

//var lodash = angular.module('lodash', []);
//lodash.factory('_', function () {
//    return window._; // assumes underscore has already been loaded on the page
//});